<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="app-container">
        <div class="box">
            <div class="pagination-container">

            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="修改头像" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="编辑头像" name="first">
                                <el-form label-position="right" label-width="100px">

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="上传图片">
                                                <el-upload
                                                        class="avatar-uploader"
                                                        action="/setmeal/upload"
                                                        :auto-upload="autoUpload"
                                                        name="imgFile"
                                                        :show-file-list="false"
                                                        :on-success="handleAvatarSuccess"
                                                        :before-upload="beforeAvatarUpload">
                                                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                </el-upload>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <!--<el-button @click="cancel()">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>-->
                        <button @click="cancel()" type="button" class="el-button el-button--danger is-circle"><i class="el-icon-circle-close"></i></button>
                        <button @click="handleEdit()" type="button" class="el-button el-button--success is-circle"><!----><i class="el-icon-check"></i><!----></button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data:{
            autoUpload:true,//自动上传
            imageUrl:null,//模型数据，用于上传图片完成后图片预览
            activeName:'first',//添加/编辑窗口Tab标签名称
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData:[],//添加表单窗口中检查组列表数据
            dialogFormVisible4Edit:true
        },
        created() {
            axios.post("/user/getimg").then((res)=>{
                if(res.data.flag){
                    this.imageUrl = "http://q1cxggodb.bkt.clouddn.com/"+ res.data.data.img;
                }
            });
        },
        methods: {
            cancel(){
                window.location.href = "/pages/main.html";
            },
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                //展示图片
                this.imageUrl = "http://q1cxggodb.bkt.clouddn.com/"+response.data;
                //提示消息
                this.$message({
                    message:response.message,
                    type:response.flag ? 'success':'error'
                });
                //给this.datafrom添加信息
                this.formData.img = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 / 1024 / 1024 < 4;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG/PNG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 4MB!');
                }
                return isJPG && isLt2M;
            },

            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
            },
            //编辑提交
            handleEdit(){
                axios.post("/user/edit?img="+this.formData.img).then((res)=>{
                    if(res.data.flag){
                        //弹出提示消息
                        this.$message({
                            message:res.data.message,
                            type:'success'
                        });
                        window.location.href = "/pages/main.html";
                    }else {
                        //弹出错误提示消息
                        this.$message.error(res.data.message);
                    }
                }).finally(()=>{
                    this.dialogFormVisible4Edit = false;
                    this.findPage();
                })
            }
        }
    })
</script>
</html>
